package com.zhuwhao.sample;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.zhuwhao.sample.app.MyApplication;
import com.zhuwhao.sample.utils.AppUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.navigationView)
    NavigationView navigationView;

    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;

    @BindView(R.id.textAlpha)
    TextView textAlpha;

    @BindView(R.id.seekBar)
    SeekBar seekBar;

    private int statusBarColor;

    private int alpha;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //设置状态栏颜色及透明度
        statusBarColor = ContextCompat.getColor(this, R.color.colorPrimary);
        StatusBarUtil.setColorForDrawerLayout(this, drawerLayout, statusBarColor, 0);
        initView();
    }

    private void initView() {
        setSupportActionBar(toolbar);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);

        textAlpha.setText("0");
        seekBar.setMax(255);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                alpha = progress;
                StatusBarUtil.setColorForDrawerLayout(MainActivity.this, drawerLayout, statusBarColor, alpha);
                textAlpha.setText(String.valueOf(alpha));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Intent intent = new Intent();
        switch (item.getItemId()) {
            case R.id.nav_date_time_picker:
                intent.setClass(this, DateTimePickerActivity.class);
                break;
            case R.id.nav_drop_down_menu:
                intent.setClass(this, DropDownMenuActivity.class);
                break;
            case R.id.nav_side_bar:
                intent.setClass(this, SideBarActivity.class);
                break;
            case R.id.nav_web_view:
                AppUtils.openUrl(this, MyApplication.getMyBlogAddress());
                drawerLayout.closeDrawer(GravityCompat.START);
                return true;
            case R.id.nav_open_source:
                intent.setClass(this, OpenSourceActivity.class);
                break;
            case R.id.nav_test:
                intent.setClass(this, TestActivity.class);
                break;
        }
        startActivity(intent);

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}